﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOABLL;
using TarreOA.TarreOAModels;

public partial class admin_SetSellManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Main.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 46)
                    {
                        userFlag = true;
                        break;
                    }
                }
            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }
        }
        if (!IsPostBack)
        {
            IList<TarreOA.TarreOAModels.User> list = UserManager.FindAll();
            foreach (TarreOA.TarreOAModels.User u in list)
            {
                if (u.ShellManager==1)
                {
                    lbSellManager.Items.Add(new ListItem("["+u.Section.SectionName+"]"+u.UserName,u.UserId.ToString()));
                }
                else
                {
                    lbSellUser.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.UserId.ToString()));
                }
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbSellManager.Items.Clear();
        lbSellUser.Items.Clear();
        IList<TarreOA.TarreOAModels.User> list = UserManager.FindAll();
        if (ddlSelectType.SelectedValue=="0")
        {
            foreach (TarreOA.TarreOAModels.User u in list)
            {
                if (u.ShellManager == 1)
                {
                    lbSellManager.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.UserId.ToString()));
                }
                else
                {
                    lbSellUser.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.UserId.ToString()));
                }
            }
        }
        else
        {
            foreach (TarreOA.TarreOAModels.User u in list)
            {
                if (u.ShellManager == 1)
                {
                    lbSellManager.Items.Add(new ListItem("[" + u.PopedomGroup.PopedomGroupName + "]" + u.UserName, u.UserId.ToString()));
                }
                else
                {
                    lbSellUser.Items.Add(new ListItem("[" + u.PopedomGroup.PopedomGroupName + "]" + u.UserName, u.UserId.ToString()));
                }
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string[] sellManagers = Request.Form.GetValues("lbSellManager");
        string[] sellUsers = Request.Form.GetValues("lbSellUser");
        if (UserManager.SetSellManager(sellManagers, sellUsers))
        {
            lbSellManager.Items.Clear();
            lbSellUser.Items.Clear();
            IList<TarreOA.TarreOAModels.User> list = UserManager.FindAll();
            if (ddlSelectType.SelectedValue == "0")
            {
                foreach (TarreOA.TarreOAModels.User u in list)
                {
                    if (u.ShellManager == 1)
                    {
                        lbSellManager.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.UserId.ToString()));
                    }
                    else
                    {
                        lbSellUser.Items.Add(new ListItem("[" + u.Section.SectionName + "]" + u.UserName, u.UserId.ToString()));
                    }
                }
            }
            else
            {
                foreach (TarreOA.TarreOAModels.User u in list)
                {
                    if (u.ShellManager == 1)
                    {
                        lbSellManager.Items.Add(new ListItem("[" + u.PopedomGroup.PopedomGroupName + "]" + u.UserName, u.UserId.ToString()));
                    }
                    else
                    {
                        lbSellUser.Items.Add(new ListItem("[" + u.PopedomGroup.PopedomGroupName + "]" + u.UserName, u.UserId.ToString()));
                    }
                }
            }
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('保存成功！')", true);
        }
        else
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "alert('保存失败！请重试！')", true);
        }
    }
}
